package com.gxjtkyy.datadistributer.writer.rdbms.config;

import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class DbConfig {
   @Bean(
      name = {"queryDataSource"}
   )
   @ConfigurationProperties(
      prefix = "spring.datasource.query"
   )
   public DataSource queryDataSource() {
      return new HikariDataSource();
   }

   @Bean({"targetDataSource"})
   @Primary
   @ConfigurationProperties(
      prefix = "spring.datasource.target"
   )
   public DataSource targetDataSource() {
      return new HikariDataSource();
   }

   @Bean(
      name = {"queryJdbcTemplate"}
   )
   public JdbcTemplate queryJdbcTemplate(@Qualifier("queryDataSource") DataSource dataSource) {
      return new JdbcTemplate(dataSource);
   }

   @Bean(
      name = {"targetJdbcTemplate"}
   )
   @Primary
   public JdbcTemplate targetJdbcTemplate(@Qualifier("targetDataSource") DataSource dataSource) {
      return new JdbcTemplate(dataSource);
   }
}
